from django.http import HttpResponse, HttpRequest, JsonResponse
from django.views.generic import View
import requests
from .models import AdminUsers as AdminUsersModels
from .models import About  as AboutModels,ArticleCategory as ArticleCategoryModel
from .lib import password_encrypt, get_token
from .restframework import JwtQuertParamsAuthentication
from rest_framework.viewsets import ModelViewSet
from .serializers import  ArticleCategorySerializer
class AdminUser(View):
    # 获取列表
    def get(self, request):
        return JsonResponse({
            'code': 1,
            'msg': "登录成功",
        })

    # 创建账号
    def post(self, request):
        print(request.method)
        id = request.POST.get('id', '')
        name = request.POST.get('name', '')
        real_name = request.POST.get('real_name', '')
        phone = request.POST.get('phone', '')
        password = request.POST.get('password', '')
        if id is not None:
            AdminUsersModels.objects.create(name=name, real_name=real_name, phone=phone,
                                            password=password_encrypt(password))
        else:
            adminUserModel = AdminUsersModels.objects.get(id=id)
            adminUserModel.name = name
            adminUserModel.real_name = real_name
            adminUserModel.phone = phone
            adminUserModel.password = password_encrypt(password)
            adminUserModel.save()
        return JsonResponse({
            'code': 1,
            'msg': "修改成功",
        })

    # 账号登录
    def login(request):
        name = request.POST.get('name', '')
        password = request.POST.get('password', '')
        try:
            AdminUsersModels.objects.get(name=name, password=password_encrypt(password))
        except AdminUsersModels.DoesNotExist:
            return JsonResponse({
                'code': 0,
                'msg': "密码错误",
            })
        adminUserResult = AdminUsersModels.objects.get(name=name, password=password_encrypt(password))
        # if list(adminUserResult) == []:
        #     return JsonResponse({
        #         'code': 0,
        #         'msg': "密码错误",
        #     })
        # 生成token
        # https://www.cnblogs.com/shangguanruoling/p/12157121.html
        # https://www.cnblogs.com/liuzhanghao/p/12686301.html
        payload = {
            "id": adminUserResult.id,
            "name": adminUserResult.name,
        }
        token = get_token(payload, 5)
        return JsonResponse({
            'code': 1,
            'msg': "登录成功-1",
            'token': token,
        })


class AboutListView(View):

    def get(self, request):
        """列表获取"""
        abouts = AboutModels.objects.all()
        abouts_list = []
        for about in abouts:
            about_dict = {
                "id": about.id,
                "address": about.address,
                "fax_number": about.fax_number,
                "record_number": about.record_number,
                "email": about.email,
                "phone": about.phone,
            }
            abouts_list.append(about_dict)

        return JsonResponse({
            'code': 1,
            'msg': "ok",
            'data': abouts_list,
        })

    def post(self, request):
        """新增"""
        return JsonResponse({
            'code': 1,
            'msg': "about post 验证{0}".format(id),
        })


class AboutDetailView(View):

    def get(self, request, id):
        """单个获取"""
        try:
            about = AboutModels.objects.get(id=id)
        except AboutModels.DoesNotExist:
            return JsonResponse({
                'code': 0,
                'msg': "不存在的数据",
            })
        about_dict = {
            "id": about.id,
            "address": about.address,
            "fax_number": about.fax_number,
            "record_number": about.record_number,
            "email": about.email,
            "phone": about.phone,
        }
        return JsonResponse({
            'code': 1,
            'msg': "ok",
            'data': about_dict,
        })

    def post(self, request, id):
        """修改"""
        return JsonResponse({
            'code': 1,
            'msg': "about post 验证{0}".format(id),
        })

    def delete(self, request, id):
        """删除"""
        return JsonResponse({
            'code': 1,
            'msg': "about delete 验证{0}".format(id),
        })


class ArticleCategoryView(ModelViewSet):
    """定义类视图"""
    queryset = ArticleCategoryModel.objects.all()
    #指定序列化器
    serializer_class = ArticleCategorySerializer